package dream.zyp.ballgame.component;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import dream.zyp.ballgame.control.BlockBumpControl;

/**
 * Created by ZYP on 2017/3/10.
 * 一组砖块
 */
public class GameBlockGroup {
    private int screenWidth;
    private int allocatedHeight;
    private int rowNumber, colNumber;
    private static final int width_interval = 30;
    private static final int height_interval = 30;
    public static final int blockWidth = 80,blockHeight = 40;
    private GameBlock[][] blocks;

    public GameBlockGroup(int screenWidth, int allocatedHeight){
        this.screenWidth = screenWidth;
        this.allocatedHeight = allocatedHeight;

        this.colNumber = (screenWidth) /
                (blockWidth + width_interval);
        this.rowNumber = (allocatedHeight - height_interval) /
                (blockHeight + height_interval);
        //Log.e("adfd", "GameBlockGroup: "+colNumber+"|"+rowNumber );
        blocks = new GameBlock[rowNumber][colNumber];

        initBlocks();
    }

    /**
     * 根据 小球坐标xy的值改变block的状态。
     * @param ballx
     * @param bally
     */
    public BlockBumpControl changeBlockState(float ballx, float bally) {
        BlockBumpControl control = new BlockBumpControl();
        int i = (int)bally / (blockHeight + height_interval);
        int j = (int)(ballx - width_interval) / (blockWidth + width_interval);
        int y = (int)(allocatedHeight - bally - height_interval);
        float remaining = (ballx - width_interval) % (blockWidth + width_interval);
        if (y < 0) {
            control.upOrDown = false;
            return control;  //说明不在范围内
        }
        int row = i, col = j;

        if (row >= rowNumber || col >= colNumber){
            control.upOrDown = false;
            return control;  //说明不在范围内
        }
        if (blocks[row][col].draw){
            blocks[row][col].draw = false;
            control.upOrDown = true;
            Log.e("EEEEEEEE", "changeBlockState: "+remaining );
            if (remaining > (blockWidth + width_interval) / 2) {
                control.leftOrRight = true;
            } else if(remaining > 0 && remaining < (blockWidth + width_interval) / 2) {
                control.leftOrRight = false;
            }
            return control;
        }
        control.upOrDown = false;
        return control;
    }
    /**
     * 初始化各个方块的信息
     */
    private void initBlocks(){
        for (int i = 0; i < rowNumber; i++){
            for (int j = 0; j < colNumber; j++){
                blocks[i][j] = new GameBlock();
                blocks[i][j].x = width_interval +
                        j * (blockWidth + width_interval);
                blocks[i][j].y = i * (blockHeight + height_interval);
            }
        }
    }

    public void drawBlocks(Canvas canvas, Paint paint){
        for (int i = 0; i < rowNumber; i++){
            for (int j = 0; j < colNumber; j++){
                if(blocks[i][j].draw){
                    canvas.drawRect(blocks[i][j].x,
                            blocks[i][j].y + blockHeight,
                            blocks[i][j].x + blockWidth,
                            blocks[i][j].y,paint);
                }

            }
        }
    }

    public GameBlock[][] getBlocks(){return blocks;}

    public int getRowNumber(){return rowNumber;}

    public int getColNumber(){return colNumber;}
}
